package cn.kai.process.flow.node;

import cn.hutool.extra.spring.SpringUtil;
import cn.kai.process.chain.CFRunChain;
import lombok.NoArgsConstructor;
import cn.kai.process.flow.core.Node;
import cn.kai.process.flow.core.NodeContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.List;
import java.util.Objects;

/**
 * 并行节点
 Author: cKnight
 Date: 2025/1/23
 */
@NoArgsConstructor
public class ParallelNode implements Node {
    private List<Node> nodes;
    /** 超时时间 */
    private Integer exprTime;

    public ParallelNode(List<Node> nodes,Integer exprTime){
        this.nodes = nodes;
        this.exprTime = Objects.isNull(exprTime) || exprTime.equals(0) ? 10 : exprTime;
    }

    @Override
    public void execute(NodeContext context) {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = SpringUtil.getBean("threadPoolTaskExecutor", ThreadPoolTaskExecutor.class);
        CFRunChain cfRunChain = CFRunChain.start(threadPoolTaskExecutor);
        for (Node node : nodes) {
            cfRunChain.next(()-> node.exe(context));
        }
        cfRunChain.end(Long.valueOf(exprTime));
    }
}
